home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / grafix / tools / amipeg_0.4 / util.c < prev    next >
C/C++ Source or Header  |  1994-04-22  |  4KB  |  202 lines

  1. /*
  2.  *  This file handles filling of the buffer and contains some additional utility routines.
  3.  */
  4.  
  5. #include "video.h"
  6. #include "proto.h"
  7. #include "util.h"
  8.  
  9. /* Declarations of global variables used. */
  10.  
  11. int bitOffset;
  12. int bufLength;
  13. unsigned int *bitBuffer;
  14.  
  15.  
  16. /*
  17.  *--------------------------------------------------------------
  18.  *
  19.  * correct_underflow --
  20.  *
  21.  *    Called when buffer does not have sufficient data to 
  22.  *      satisfy request for bits.
  23.  *      Calls get_more_data, an application specific routine
  24.  *      required to fill the buffer with more data.
  25.  *
  26.  * Results:
  27.  *      None really.
  28.  *  
  29.  * Side effects:
  30.  *    buf_length and buffer fields in curVidStream structure
  31.  *      may be changed.
  32.  *
  33.  *--------------------------------------------------------------
  34.  */
  35.  
  36. void correct_underflow(void)
  37. {
  38.   int status;
  39.  
  40.   status = get_more_data(curVidStream->buf_start, curVidStream->max_buf_length, &bufLength, &bitBuffer);
  41.  
  42.   if (status  < 0) {
  43.     if (!quietFlag) {
  44.       fprintf (stderr, "\n");
  45.       perror("Unexpected read error.");
  46.     }
  47.     exit(1);
  48.   }
  49.   else if ((status == 0) && (bufLength < 1)) {
  50.     if (!quietFlag) {
  51.       fprintf(stderr, "\nImproper or missing sequence end code.\n");
  52.     }
  53. #ifdef ANALYSIS
  54.     PrintAllStats();
  55. #endif
  56.     if (!quietFlag) {
  57.       PrintTimeInfo();
  58.     }
  59.  
  60.     if (loopFlag) longjmp(env, 1);
  61.     DestroyVidStream(curVidStream);
  62.     exit(0);
  63.   }
  64.  
  65. }
  66.  
  67.  
  68. /*
  69.  *--------------------------------------------------------------
  70.  *
  71.  * get_ext_data --
  72.  *
  73.  *    Assumes that bit stream is at begining of extension
  74.  *      data. Parses off extension data into dynamically 
  75.  *      allocated space until start code is hit. 
  76.  *
  77.  * Results:
  78.  *    Pointer to dynamically allocated memory containing
  79.  *      extension data.
  80.  *
  81.  * Side effects:
  82.  *    Bit stream irreversibly parsed.
  83.  *
  84.  * Niceness -10000 !!!!
  85.  *
  86.  *--------------------------------------------------------------
  87.  */
  88.  
  89. char *get_ext_data (void)
  90. {
  91.   int size, marker;
  92.   char *dataPtr;
  93.   unsigned int data;
  94.  
  95.   /* Set initial ext data buffer size. */
  96.  
  97.   size = EXT_BUF_SIZE;
  98.  
  99.   /* Allocate ext data buffer. */
  100.  
  101.   dataPtr = (char *) malloc(size);
  102.  
  103.   /* Initialize marker to keep place in ext data buffer. */
  104.  
  105.   marker = 0;
  106.  
  107.   /* While next data is not start code... */
  108.   while ((show_bitsn(24,data)) != 0x000001) {                /* data re-usage could be applied ! */
  109.  
  110.     /* Put ext data into ext data buffer. Advance marker. */
  111.  
  112.     get_bits8( dataPtr[marker] );
  113.     marker++;
  114.  
  115.     /* If end of ext data buffer reached, resize data buffer. */
  116.  
  117.     if (marker == size) {
  118.       size += EXT_BUF_SIZE;
  119.       dataPtr = (char *) realloc(dataPtr, size);
  120.     }
  121.   }
  122.  
  123.   /* Realloc data buffer to free any extra space. */
  124.  
  125.   dataPtr = (char *) realloc(dataPtr, marker);
  126.  
  127.   /* Return pointer to ext data buffer. */
  128.  
  129.   return dataPtr;
  130. }
  131.  
  132.  
  133. /*
  134.  *--------------------------------------------------------------
  135.  *
  136.  * get_extra_bit_info --
  137.  *
  138.  *    Parses off extra bit info stream into dynamically 
  139.  *      allocated memory. Extra bit info is indicated by
  140.  *      a flag bit set to 1, followed by 8 bits of data.
  141.  *      This continues until the flag bit is zero. Assumes
  142.  *      that bit stream set to first flag bit in extra
  143.  *      bit info stream.
  144.  *
  145.  * Results:
  146.  *    Pointer to dynamically allocated memory with extra
  147.  *      bit info in it. Flag bits are NOT included.
  148.  *
  149.  * Side effects:
  150.  *    Bit stream irreversibly parsed.
  151.  *
  152.  *--------------------------------------------------------------
  153.  */
  154.  
  155. char *get_extra_bit_info (void)
  156. {
  157.   int size, marker;
  158.   char *dataPtr;
  159.   unsigned int data;
  160.  
  161.   /* Get first flag bit; if flag is false, return NULL pointer (i.e. no extra bit info). */
  162.  
  163.   if (!(get_bits1(data))) return NULL;
  164.  
  165.   /* Initialize size of extra bit info buffer and allocate. */
  166.  
  167.   size = EXT_BUF_SIZE;
  168.   dataPtr = (char *) malloc(size);
  169.  
  170.   /* Reset marker to hold place in buffer. */
  171.  
  172.   marker = 0;
  173.  
  174.   /* While flag bit is true. */
  175.  
  176.   while (data) {
  177.  
  178.     /* Place in extra bit info buffer. */
  179.  
  180.     get_bits8( dataPtr[marker] );
  181.     marker++;
  182.  
  183.     /* If buffer is full, reallocate. */
  184.  
  185.     if (marker == size) {
  186.       size += EXT_BUF_SIZE;
  187.       dataPtr = (char *) realloc(dataPtr, size);
  188.     }
  189.  
  190.     /* Get next flag bit. */
  191.     get_bits1(data);
  192.   }
  193.  
  194.   /* Reallocate buffer to free extra space. */
  195.  
  196.   dataPtr = (char *) realloc(dataPtr, marker);
  197.  
  198.   /* Return pointer to extra bit info buffer. */
  199.  
  200.   return dataPtr;
  201. }
  202.